﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.ComponentModel;

namespace ReklamaWPF
{
    /// <summary>
    /// Interaction logic for SelectId.xaml
    /// </summary>
    public partial class SelectId : Window
    {
        public string Id = null;
        public DataTable tb;
      //  Predicate<object> filterFx;

        public SelectId()
        {
            InitializeComponent();
        }

        public void LoadData(string sql){
            DataBase db = new DataBase("192.168.100.100\\SQLEXPRESS", "sa", "tornado", "PASystem");
            tb = db.getTable(sql);
            dataGrid1.ItemsSource=tb.AsDataView();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        private void dataGrid1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            Id = null;
            if (dataGrid1.SelectedIndex > -1)
            {
                DataRowView row = (DataRowView)dataGrid1.SelectedItems[0];
                Id=row["Id"].ToString();
            }
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            //ICollectionView view = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource);
            BindingListCollectionView view = (BindingListCollectionView)CollectionViewSource.GetDefaultView(tb.DefaultView);
            bool cf = view.CanFilter;
            view.CustomFilter = "Name Like '%" + textBox1.Text.ToString() + "%'";
            dataGrid1.ItemsSource = view;
            //cv.Filter = new System.Predicate<Object>(o => ((TableName)o).Name.Contains(textBox1.Text.ToString()));
           // view.Filter= delegate(object item){
            //                  return item.Contains(textBox1.Text.ToString());
           //                 };
           // this.filterFx = (p) => ((string)p).ToUpper().Contains(this.textBox1.Text.ToUpper());
            //view.Filter = this.filterFx;
          //  ICollectionView view = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource);
          // IEqualityComparer<string> comparer = StringComparer.InvariantCultureIgnoreCase;
          // view.Filter = o =>
          // {
          //     TableName p = o as TableName;
          //     return p.Name.Contains(textBox1.Text.ToString());
          // };

          // IBindingListView blv = tb.DefaultView;
           //blv.Filter = "Name Like '%Miestai%'";
           //dataGrid1.ItemsSource = tb.AsDataView();
           //ApplyFilters();
        }

        public void ApplyFilters()
        {
            ICollectionView view = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource);
            if (view != null)
            {
                view.Filter = FilterPredicate;
            }
        }

        private bool FilterPredicate(object item)
        {
            var row= item as TableName;

            return row.Name.Contains(textBox1.Text.ToString());
        }

        public class TableName
        {
            public string id { get; set; }
            public String Name { get; set; }
        }
    }
}
